%% Legal blahblah
% Copyright 2008 Alex Rattner
% This file is part of Pursuit.
% 
% Pursuit is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% Foobar is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with Pursuit.  If not, see <http://www.gnu.org/licenses/>.
%%

function [accel, alpha] = Gazelle(me, him)
%For now we will try direct flee (probably the worst solution)
accel = me.accelMax;

him.x = him.x - me.x;
him.y = him.y - me.y;

%aim directly away from him
thetaGoal = pi + atan2(him.y, him.x);
thetaGoal = mod(thetaGoal, 2*pi);

%pick shorter direction
deltaTheta = thetaGoal-me.theta;
if (deltaTheta > pi)
    deltaTheta = deltaTheta - 2*pi;
elseif(deltaTheta < -pi)
    deltaTheta = deltaTheta + 2*pi;
end

%use a PI controller to get to goal angle
a = 1; b = 1; %gains
alpha = a*deltaTheta - b*me.omega;

%only give possible responses
alpha = median([me.alphaMin alpha me.alphaMax]);